<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/*********************
*@param Function Description:後台管理控制器
*@param time: 2011-3-16
*@param Author:abin
*********************/
class Admins_Controller extends CI_Controller{
  public $settings=array();
  public function __construct() {
    parent::__construct();
    $this->check_admin_online();
	  $this->setLang();
    $this->get_settings();//獲取設置
    //獲取列表篩選參數
    $cookie_url_query=$this->input->cookie('url_query');
    $this->settings['url_query']=!empty($cookie_url_query)?'?'.base64_decode($cookie_url_query):'';
  }
  protected function check_admin_online(){
    $this->load->model('admins_mdl');
    $admin_id=$this->nsession->userdata('admin_id');
    $group_id=$this->nsession->userdata('group_id');
    $result=$this->admins_mdl->check_online($admin_id);
    if($result && !empty($group_id)){
      $this->load->model('admins_group_mdl');
      $query=$this->admins_group_mdl->get_admins_group_by_id($group_id); //獲取用戶組權限
      $urls=$this->uri->segment(2,0);//獲取操作段
      $permissions=unserialize($query['permission']);
      if(!is_array($permissions) || !in_array($urls,$permissions)){
        redirect('admins/index/permission');
      }
    }else{
      redirect('admins/login?ref='.urlencode($this->uri->uri_string()));
    }
  }
  protected function get_settings(){
    $this->load->model('settings_mdl');
    $query=$this->settings_mdl->get_core_settings(array('no_group'=>'other'));
    foreach($query->result_array() as $q){
      $this->settings[$q['key']]=$q['value'];
    }
  }
  //操作日誌
  public function operator_log($title='',$action='',$result=1){
    $this->load->model('operator_mdl');
    if(isset($this->settings['operator_log']) && $this->settings['operator_log']>0){
      $expired=strtotime('-'.$this->settings['operator_log'].' day');
      $this->operator_mdl->delete_logs($expired);
      $data=array(
        'operator'=>$this->nsession->userdata('nick_name'),
        'title'=>$title,
        'action'=>$action,
        'urls'=>$this->uri->uri_string(),
        'result'=>$result>0?'成功':'失敗',
        'add_time'=>time()
      );
      $this->operator_mdl->add_operator($data);
    }
  }
  //獲取客戶反饋
  public function get_feedbooks(){
    $this->load->model('feedbacks_mdl');
    $data=array(
        'language_id'=>$this->settings['default_language'],
        'is_view'=>1,//未讀
        'field'=>'add_time',
        'sort'=>'desc'
    );
    return $this->feedbacks_mdl->totle_feedbacks($data);
  }
  
  public function setLang(){
	      $lan_get = 	$this->input->get('lan','');
        $lan_get = empty($lan_get) ? get_cookie('language') : $lan_get;
        if(empty($lan_get) || !in_array($lan_get,array('zh-tw','english'))){
            $lan_get = 'zh-tw';
        }
       
        $this->lang->load('page', $lan_get);
        set_cookie('language',$lan_get,time()+24*3600*30);
        $this->load->model('settings_mdl');
        if($lan_get=='zh-tw'){
            $this->settings_mdl->edit_settings_by_key(array('value'=>1),'default_language');
        }elseif($lan_get=='english'){
            $this->settings_mdl->edit_settings_by_key(array('value'=>2),'default_language');
        }
        $this->settings['getlang'] = $lan_get;
        $this->settings['lang'] =$this->lang->language;

  }
  
}
/*********************
*@param Function Description:前台管理語言控制器
*@param time: 2011-3-16
*@param Author:abin
*********************/
class Lang_Controller extends CI_Controller{
  public $langs_array=array();
  public $data=array();
  public function __construct(){
    parent::__construct();
    $this->get_settings();//獲取設置
    $this->data['session_id'] = $this->nsession->userdata('session_id');
    //檢查網站是否在維修.
    if($this->data['website_status']!=1){
      show_error('Sorry, website maintenance, please visit again later!(對不起,網站維護中請稍後訪問!)');
      exit;
    }
    // $lang_id=$this->input->cookie('language_id');
    // $lang_id=!empty($lang_id)?$lang_id:$this->data['default_language'];
    $lang_id=$this->data['default_language'];
    $this->load->model('languages_mdl');
    $query=$this->languages_mdl->get_languages_by_id($lang_id);
    if (empty($query) || !is_array($query) || count($query)<=0)
      $query=$this->languages_mdl->get_languages_by_id($this->data['default_language']);
    $this->data['lang_id']=$query['id'];
    $this->data['lang']=$query['filename'];
    $this->config->set_item('language',$query['filename']); //默認出現的語言.
    $this->load->language('header',$query['filename']);
    $this->load->language('global',$query['filename']);
    $this->load->language('footer',$query['filename']);
    //執行網站統計
    $this->website_totle();
    //獲取menu資訊
   // $this->data['CIabouts']=$this->get_CI_menu('abouts'); //abouts
   // $this->data['CIguides']=$this->get_CI_menu('guides'); //abouts
   // $this->data['CIrenttype']=$this->get_CI_menu('renttype');//renttype
   // $this->data['CIworkshoptype']=$this->get_CI_menu('workshoptype'); //workshoptype
   // $this->data['CIeventtype']=$this->get_CI_menu('eventtype'); //分類
    //獲取語言
//    $this->data['languages']=$this->get_languages();
//    $this->_get_adv('home','banner2');
  }

  //獲取配置文件.
  protected function get_settings(){
    $this->load->model('settings_mdl');
    // $query=$this->settings_mdl->get_core_settings(array('no_group'=>'other'));
    $query=$this->settings_mdl->get_core_settings();
    foreach($query->result_array() as $q){
      $this->data[$q['key']]=$q['value'];
      if($q['key']=='title'){
        $this->data['web_title']=$q['value'];
      }
    }
  }
  //獲取語言
  public function get_languages(){
    $query=$this->languages_mdl->get_core_languages(array('status'=>1));
    return $query;
  }
  //執行網站統計
  public function website_totle(){
    $this->load->helper('count');
    website_count();
  }
  //獲取cms Page
  public function get_CI_menu($page='',$parent=0){
    if(!empty($page)){
      $mdl=$page.'_mdl';
      $this->load->model($mdl);
      $data=array('language_id'=>$this->data['lang_id'],'parent'=>$parent,'status'=>1);
      $fun='get_'.$page;
      $query=$this->$mdl->$fun($data);
      return $query;
    }
  }
  //獲取幻燈片圖片
  private function _get_adv($var='home',$position='banner1'){
    if(!empty($var)){
      $this->load->model('advertisings_mdl');
      $filter=array('category'=>$var,'status'=>1,'position'=>$position);
      $this->data[$position]=$this->advertisings_mdl->get_advertisings($filter);
    }
  }

}
/*********************
*@param Function Description:前台會員登錄判斷.
*@param time: 2011-3-16
*@param Author:abin
*********************/
class Front_Controller extends Lang_Controller{
  public function __construct() {
    parent::__construct();
    $this->load->model('members_mdl');
    $this->check_user_online();
  }
  protected function check_user_online(){
    $member_id=$this->nsession->userdata('member_id');
    $result=$this->members_mdl->check_online($member_id);
    if(!$result){
       show_error('對不起,請您先登入!');
       exit;
    }
  }
}
